package net.cyl.exception;

import lombok.extern.slf4j.Slf4j;
import net.cyl.util.JsonData;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@Slf4j
public class CustomExceptionHandler {
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public JsonData handlerException(Exception e) {
        if (e instanceof BizException) {
            BizException bizException = (BizException) e;
            log.error("业务异常:{},{}", bizException.getCode(), bizException.getMsg());
            return JsonData.buildCodeAndMsg(bizException.getCode(), bizException.getMsg());
        }
        log.error("系统异常:{}", e.getMessage());
        e.printStackTrace();
        return JsonData.buildError(e.getMessage());
    }
}
